<?php

/*
 * Current known limitations:
 *   - Can only works with the default "messages" catalogue
 *   - For file backends (XLIFF and gettext), it only saves/deletes strings in the "most global" file
 */

/*
 * This file is part of the symfony package.
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
 * 
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * Extracts i18n strings from php files.
 *
 * @package    symfony
 * @subpackage task
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @version    SVN: $Id: sfI18nExtractTask.class.php 9883 2008-06-26 09:04:13Z FabianLange $
 */
class sfI18nFillTask extends sfBaseTask
{
  /**
   * @see sfTask
   */
  protected function configure()
  {
    $this->addArguments(array(
      new sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'),
    ));

    $this->namespace = 'i18n';
    $this->name = 'fill';
    $this->briefDescription = 'Update the i18n message file';

    /*$this->detailedDescription = <<<EOF
The [i18n:extract|INFO] task extracts i18n strings from your project files
for the given application and target culture:

  [./symfony i18n:extract frontend|INFO]

By default, the task only displays the number of new and old strings
it found in the current project.

If you want to display the new strings, use the [--display-new|COMMENT] option:

  [./symfony i18n:extract --display-new frontend|INFO]

To save them in the i18n message catalogue, use the [--auto-save|COMMENT] option:

  [./symfony i18n:extract --auto-save frontend|INFO]

If you want to display strings that are present in the i18n messages
catalogue but are not found in the application, use the 
[--display-old|COMMENT] option:

  [./symfony i18n:extract --display-old frontend|INFO]

To automatically delete old strings, use the [--auto-delete|COMMENT] but
be careful, especially if you have translations for plugins as they will
appear as old strings but they are not:

  [./symfony i18n:extract --auto-delete frontend|INFO]
EOF;*/
  }

  /**
   * @see sfTask
   */
  public function execute($arguments = array(), $options = array())
  {
    $this->logSection('i18n', sprintf('extracting i18n strings for the "%s" application', $arguments['application']));

    $filename = sfConfig::get( 'sf_app_i18n_dir' ) . '/en/messages.xml';
    
    $xml = simplexml_load_file( $filename );
   
        $messages = array();
    foreach( $xml->file->body->{'trans-unit'} as $message ) {
        $messages[] = (string) $message->source;
    }
    
    $i18n = ToolbaseI18N::getInstance();
    $messages_xml = $i18n->getMessages();
    
    foreach( $messages as $message ) {
        if( !in_array( $message, $messages_xml['en'] ) ) {
                $messages_xml['en'][ 'cyberpowertoolbase-' . ( count( $messages_xml['en'] ) + 1 ) ] = $message;
        }
    }
    
    file_put_contents( sfConfig::get( 'sf_app_i18n_dir' ) . '/ToolbaseMessages.i18n.php', sprintf( "<?php\n\n\$messages = %s;", var_export( $messages_xml, true ) ) );
    
  }
}
